package com.example.a12138.myapplication;

import android.content.Intent;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.example.a12138.myapplication.fragment.BookFragment;
import com.example.a12138.myapplication.fragment.MeFragment;
import com.example.a12138.myapplication.fragment.MsgFragment;
import com.example.a12138.myapplication.fragment.RealinfoFragment;

public class Main2Activity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener{
    private RadioGroup rg;
    private RealinfoFragment realinfoFragment;
    private BookFragment bookFragment;
    private MsgFragment msgFragment;
    private MeFragment meFragment;
    private FragmentManager fm;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        initFraments();
        hideFragments();
        rg=findViewById(R.id.rg);
       /*rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {

            }
        });*/
       rg.setOnCheckedChangeListener(this);
        Intent intent=getIntent();
        if(intent!=null) {
            Bundle bundle= intent.getExtras();
            if(bundle!=null) {
                String username=bundle.getString("username");
                Toast.makeText(this, "欢迎"+username+"来到德莱联盟", Toast.LENGTH_SHORT).show();
            }

        }
        ((RadioButton)(rg.getChildAt(0))).setChecked(true);

    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
        hideFragments();
        FragmentTransaction ft=fm.beginTransaction();
        switch (checkedId) {
            case R.id.rbRealinfo:
                Toast.makeText(this, "切换资讯页面", Toast.LENGTH_SHORT).show();
                ft.show(realinfoFragment);
                break;
            case R.id.rbBook:
                Toast.makeText(this, "切换图书页面", Toast.LENGTH_SHORT).show();
                ft.show(bookFragment);
                break;
            case R.id.rbMsg:
                Toast.makeText(this, "切换留言页面", Toast.LENGTH_SHORT).show();
                ft.show(msgFragment);
                break;
            case R.id.rbMe:
                Toast.makeText(this, "切换我的页面", Toast.LENGTH_SHORT).show();
                ft.show(meFragment);
                break;
        }
        ft.commit();
    }
    private void initFraments(){
        realinfoFragment=new RealinfoFragment();
        bookFragment=new BookFragment();
        msgFragment=new MsgFragment();
        meFragment=new MeFragment();
        fm=getSupportFragmentManager();
        FragmentTransaction ft=fm.beginTransaction();
        ft.add(R.id.f1,realinfoFragment);
        ft.add(R.id.f1,bookFragment);
        ft.add(R.id.f1,msgFragment);
        ft.add(R.id.f1,meFragment);
        ft.commit();
    }
    private void hideFragments(){
        FragmentTransaction ft=fm.beginTransaction();
        ft.hide(realinfoFragment);
        ft.hide(bookFragment);
        ft.hide(msgFragment);
        ft.hide(meFragment);
        ft.commit();
    }
}
